package com.example;

import org.springframework.jdbc.core.JdbcTemplate;
public class RegistrationService
{

    private final JdbcTemplate jdbcTemplate;

    public RegistrationService(JdbcTemplate jdbcTemplate)
    {

        this.jdbcTemplate = jdbcTemplate;
    }

    public boolean registerAccount(String username, String password)
    {
        // 检查账号是否已存在
        String checkAccountSql = "SELECT COUNT(*) FROM user WHERE username = ?";
        Integer count = jdbcTemplate.queryForObject(checkAccountSql, new Object[]{username}, Integer.class);
        if (count != null && count > 0)
        {
            // 账号已存在
            return false;
        }

        // 保存账号信息到数据库
        String saveAccountSql = "INSERT INTO user (username, password) VALUES (?, ?)";
        int rowsAffected = jdbcTemplate.update(saveAccountSql, username, password);
        return rowsAffected > 0; // 如果插入成功，返回true
    }
}